{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2452213855824\n",
      "2451570734800\n",
      "2451570734800\n"
     ]
    }
   ],
   "source": [
    "import numpy as np #导入numpy  \n",
    "  \n",
    "lst = np.array([2,1,3]) #构建数组  \n",
    "\n",
    "#打印数组内每个元素的内存地址  \n",
    "for i in range(len(lst)):  \n",
    "    print(id(lst[i]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['aaa' 'bbb' 'ccc']\n",
      "[[1 2]\n",
      " [2 3]\n",
      " [3 4]]\n",
      "[2 1 3]\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 12\u001b[0m\n\u001b[0;32m      9\u001b[0m c_lst \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m3\u001b[39m])\n\u001b[0;32m     10\u001b[0m \u001b[38;5;28mprint\u001b[39m(c_lst)\n\u001b[1;32m---> 12\u001b[0m d_lst \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marray\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43maaa\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     13\u001b[0m \u001b[38;5;28mprint\u001b[39m(d_lst)\n",
      "\u001b[1;31mValueError\u001b[0m: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (3,) + inhomogeneous part."
     ]
    }
   ],
   "source": [
    "import numpy as np #导入numpy \n",
    "\n",
    "a_lst = np.array([\"aaa\",\"bbb\",\"ccc\"])\n",
    "print(a_lst)\n",
    "\n",
    "b_lst = np.array([[1,2],[2,3],[3,4]])\n",
    "print(b_lst)\n",
    "\n",
    "c_lst = np.array([2,1,3])\n",
    "print(c_lst)\n",
    "\n",
    "d_lst = np.array([2,\"aaa\",[1,2]])\n",
    "print(d_lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "import numpy as np #导入numpy\n",
    "\n",
    "a = [2,1,3]\n",
    "a_ary = np.array(a)\n",
    "\n",
    "print(type(a))\n",
    "print(type(a_ary))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "[5 1 3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np #导入numpy\n",
    "\n",
    "a_ary = np.array([2,1,3])\n",
    "print(a_ary[0])\n",
    "\n",
    "a_ary[0] = 5\n",
    "print(a_ary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 2, 3, 4]\n",
      "[3 5 7]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np #导入numpy \n",
    "\n",
    "a_lst = [1,2,3]\n",
    "b_lst = [2,3,4]\n",
    "\n",
    "a_ary = np.array(a_lst)\n",
    "b_ary = np.array(b_lst)\n",
    "\n",
    "print(a_lst + b_lst)\n",
    "print(a_ary + b_ary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List: 3 5 7 2 6 \n"
     ]
    }
   ],
   "source": [
    "class Node:  \n",
    "    def __init__(self, data=None):  \n",
    "        self.data = data  \n",
    "        self.next = None \n",
    "  \n",
    "class LinkedList:  \n",
    "    def __init__(self):  \n",
    "        self.head = None \n",
    "  \n",
    "    #创建链表  \n",
    "    def createList(self, n):  \n",
    "        for i in range(n):  \n",
    "            node = Node(int(input(f\"Enter data for node {i + 1}: \")))  \n",
    "            if self.head is None:  \n",
    "                self.head = node  \n",
    "            else:  \n",
    "                current = self.head  \n",
    "                while current.next is not None:  \n",
    "                    current = current.next  \n",
    "                current.next = node  \n",
    "  \n",
    "    #输出链表  \n",
    "    def printList(self):  \n",
    "        current = self.head  \n",
    "        print(\"List: \", end=\"\")  \n",
    "        while current is not None:  \n",
    "            print(current.data, end=\" \")  \n",
    "            current = current.next  \n",
    "        print()  \n",
    "\n",
    "    #链表长度\n",
    "    def length(self):\n",
    "        position = self.head\n",
    "        count = 0\n",
    "        while position:\n",
    "            count += 1\n",
    "            position = position.next\n",
    "        return count  \n",
    "    \n",
    "    #增加头部元素  \n",
    "    def prepend(self, data):      \n",
    "        new_node = Node(data)      \n",
    "        new_node.next = self.head      \n",
    "        self.head = new_node\n",
    "\n",
    "    #增加尾部元素\n",
    "    def append(self, data):  \n",
    "        new_node = Node(data)  \n",
    "        if self.head is None:  # 如果链表为空，则将新节点设置为头部节点  \n",
    "            self.head = new_node  \n",
    "        else:  \n",
    "            current = self.head  \n",
    "            while current.next is not None:  # 遍历链表找到最后一个节点  \n",
    "                current = current.next  \n",
    "            current.next = new_node  # 将最后一个节点的指针指向新节点\n",
    "    \n",
    "    #增加中间元素\n",
    "    def insert(self, data, position):  \n",
    "        if position <= 0:\n",
    "            self.prepend(data)\n",
    "        elif position >=self.length():\n",
    "            self.append(position)\n",
    "        else:\n",
    "            pre = self.head\n",
    "            count = 0\n",
    "            while count < position - 1:\n",
    "                count += 1\n",
    "                pre = pre.next\n",
    "            node = Node(data)\n",
    "            node.next = pre.next\n",
    "            pre.next = node\n",
    "\n",
    "    #删除头部节点\n",
    "    def delete_head(self):  \n",
    "        if self.head is not None:  \n",
    "            self.head = self.head.next  \n",
    "        else:  \n",
    "            print(\"链表为空，无法删除头部节点\")  \n",
    "    \n",
    "    #删除中间节点\n",
    "    def delete_middle(self, key):  \n",
    "        if self.head is None:  \n",
    "            print(\"链表为空，无法删除中间节点\")  \n",
    "            return  \n",
    "        temp = self.head  \n",
    "        count = 0  \n",
    "        while temp is not None and count < key:  \n",
    "            temp = temp.next  \n",
    "            count += 1  \n",
    "        if temp is None:  \n",
    "            print(\"链表长度小于给定的中间节点位置\")  \n",
    "        elif temp.next is None:  \n",
    "            print(\"给定的位置是最后一个节点，无法删除\")  \n",
    "        else:  \n",
    "            temp.next = temp.next.next  \n",
    "            return  \n",
    "    \n",
    "    #删除尾部节点\n",
    "    def delete_end(self):  \n",
    "        if self.head is None:  \n",
    "            print(\"链表为空，无法删除尾部节点\")  \n",
    "            return  \n",
    "        temp = self.head  \n",
    "        while temp.next is not None:  \n",
    "            temp = temp.next  \n",
    "        temp.next = None  \n",
    "        return\n",
    "    \n",
    "    #查找元素位置\n",
    "    def find_position(self, data):  \n",
    "        current = self.head  \n",
    "        position = 0  \n",
    "        while current is not None:  \n",
    "            if current.data == data:  \n",
    "                return position  \n",
    "            current = current.next  \n",
    "            position += 1  \n",
    "        return -1  # 如果未找到，则返回-1\n",
    "\n",
    "if __name__ == \"__main__\":  \n",
    "    n = int(input(\"Enter the number of nodes: \")) #输入节点个数  \n",
    "    linked_list = LinkedList()  \n",
    "    linked_list.createList(n)  \n",
    "    linked_list.printList()  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List: 8 3 5 7 2 6 \n"
     ]
    }
   ],
   "source": [
    "# 使用同一个链表对象进行操作  \n",
    "linked_list.prepend(8)  \n",
    "linked_list.printList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List: 8 3 5 7 2 6 4 \n"
     ]
    }
   ],
   "source": [
    "linked_list.append(4)\n",
    "linked_list.printList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List: 8 3 5 9 7 2 6 4 \n"
     ]
    }
   ],
   "source": [
    "linked_list.insert(9,3)\n",
    "linked_list.printList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List: 3 5 9 7 2 6 4 \n"
     ]
    }
   ],
   "source": [
    "linked_list.delete_head()\n",
    "linked_list.printList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List: 3 5 9 7 2 6 4 \n"
     ]
    }
   ],
   "source": [
    "linked_list.delete_end()\n",
    "linked_list.printList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List: 3 5 7 2 6 4 \n"
     ]
    }
   ],
   "source": [
    "linked_list.delete_middle(1)\n",
    "linked_list.printList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linked_list.find_position(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Student:  \n",
    "    def __init__(self, name, age, gender, student_id):  \n",
    "        self.name = name  \n",
    "        self.age = age  \n",
    "        self.gender = gender  \n",
    "        self.student_id = student_id\n",
    "        self.next = None  \n",
    "  \n",
    "    def __str__(self):  \n",
    "        return f'{self.name} ({self.gender}), {self.age}岁, 学号: {self.student_id}'  \n",
    "  \n",
    "  \n",
    "class LinkedList:  \n",
    "    def __init__(self):  \n",
    "        self.head = None  \n",
    "  \n",
    "    def add_student(self, student):  \n",
    "        if not self.head:  \n",
    "            self.head = student  \n",
    "        else:  \n",
    "            current = self.head  \n",
    "            while current.next:  \n",
    "                current = current.next  \n",
    "            current.next = student  \n",
    "  \n",
    "    def remove_student(self, name):  \n",
    "        current = self.head  \n",
    "        if current and current.name == name:  \n",
    "            self.head = current.next  \n",
    "            current = None  \n",
    "            return True  \n",
    "        prev = None  \n",
    "        while current and current.name != name:  \n",
    "            prev = current  \n",
    "            current = current.next  \n",
    "        if current:  \n",
    "            prev.next = current.next  \n",
    "            current = None  \n",
    "            return True  \n",
    "        return False  \n",
    "  \n",
    "    def update_student(self, name, age, gender):  \n",
    "        current = self.head  \n",
    "        while current and current.name != name:  \n",
    "            current = current.next  \n",
    "        if current:  \n",
    "            current.age = age  \n",
    "            current.gender = gender  \n",
    "            return True  \n",
    "        return False  \n",
    "  \n",
    "    def find_student(self, name):  \n",
    "        current = self.head  \n",
    "        while current and current.name != name:  \n",
    "            current = current.next  \n",
    "        if current:  \n",
    "            return current  \n",
    "        return None\n",
    "    \n",
    "    def print_student_list(student_list):  \n",
    "        current = student_list.head  \n",
    "        while current:  \n",
    "            print(current)  \n",
    "            current = current.next  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三 (男), 20岁, 学号: 1001\n",
      "李四 (女), 21岁, 学号: 1002\n",
      "王五 (男), 19岁, 学号: 1003\n"
     ]
    }
   ],
   "source": [
    "# 创建链表对象  \n",
    "student_list = LinkedList()  \n",
    "  \n",
    "# 添加学生对象到链表  \n",
    "student1 = Student(\"张三\", 20, \"男\", \"1001\")  \n",
    "student2 = Student(\"李四\", 21, \"女\", \"1002\")  \n",
    "student3 = Student(\"王五\", 19, \"男\", \"1003\")  \n",
    "student_list.add_student(student1)  \n",
    "student_list.add_student(student2)  \n",
    "student_list.add_student(student3)\n",
    "student_list.print_student_list()  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三 (男), 20岁, 学号: 1001\n",
      "李四 (女), 21岁, 学号: 1002\n",
      "王五 (男), 19岁, 学号: 1003\n",
      "---------------------------------------------------------------------\n",
      "张三 (女), 19岁, 学号: 1001\n",
      "李四 (女), 21岁, 学号: 1002\n",
      "王五 (男), 19岁, 学号: 1003\n"
     ]
    }
   ],
   "source": [
    "# 创建链表对象  \n",
    "student_list = LinkedList()  \n",
    "  \n",
    "# 添加学生对象到链表  \n",
    "student1 = Student(\"张三\", 20, \"男\", \"1001\")  \n",
    "student2 = Student(\"李四\", 21, \"女\", \"1002\")  \n",
    "student3 = Student(\"王五\", 19, \"男\", \"1003\")  \n",
    "student_list.add_student(student1)  \n",
    "student_list.add_student(student2)  \n",
    "student_list.add_student(student3)\n",
    "student_list.print_student_list()  \n",
    "\n",
    "print(\"---------------------------------------------------------------------\")\\\n",
    "\n",
    "student_list.update_student(\"张三\",19,\"女\",)\n",
    "student_list.remove_student(student2)\n",
    "student_list.print_student_list()  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "插入元素到队尾\n",
      "删除队头元素并返回该元素\n",
      "删除的元素：5\n",
      "队列中的元素：deque([2, 1])\n",
      "遍历队列中的元素\n",
      "2\n",
      "1\n",
      "deque([])\n"
     ]
    }
   ],
   "source": [
    "import queue  \n",
    "  \n",
    "# 创建一个队列  \n",
    "q = queue.Queue()  \n",
    "  \n",
    "print(\"插入元素到队尾\")  \n",
    "q.put(5)  \n",
    "q.put(2)  \n",
    "q.put(1)  \n",
    "  \n",
    "print(\"删除队头元素并返回该元素\")  \n",
    "item = q.get()  \n",
    "print(\"删除的元素：\" + str(item))  \n",
    "print(\"队列中的元素：\" + str(q.queue))\n",
    "\n",
    "print(\"遍历队列中的元素\")\n",
    "while not q.empty():  \n",
    "    item = q.get()  \n",
    "    print(item)\n",
    "\n",
    "print(q.queue)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在前端插入元素后: ['b', 'a']\n",
      "在后端插入元素后: ['b', 'a', 'c', 'd']\n",
      "从前端删除元素后: ['a', 'c', 'd']\n",
      "从后端删除元素后: ['a', 'c']\n"
     ]
    }
   ],
   "source": [
    "from collections import deque  \n",
    "    \n",
    "dq = deque()  \n",
    "    \n",
    "dq.appendleft('a')  # 在前端插入 'a'  \n",
    "dq.appendleft('b')  # 在前端插入 'b'，现在队列为 ['b', 'a']  \n",
    "    \n",
    "print(\"在前端插入元素后:\", list(dq))  \n",
    "  \n",
    "dq.append('c')  # 在后端插入 'c'  \n",
    "dq.append('d')  # 在后端插入 'd'，现在队列为 ['b', 'a', 'c', 'd']  \n",
    "  \n",
    "print(\"在后端插入元素后:\", list(dq))  \n",
    "  \n",
    "dq.popleft()  # 删除并返回前端的元素 'b'  \n",
    "  \n",
    "print(\"从前端删除元素后:\", list(dq))  \n",
    "   \n",
    "dq.pop()  # 删除并返回后端的元素 'd'  \n",
    "  \n",
    "print(\"从后端删除元素后:\", list(dq))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在队列的右侧添加元素\n",
      "deque([5, 2])\n",
      "在队列的左侧添加元素\n",
      "deque([1, 5, 2])\n",
      "从队列的右侧删除元素并返回该元素\n",
      "2\n",
      "从队列的左侧删除元素并返回该元素\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "from collections import deque  \n",
    "  \n",
    "# 创建一个空的双端队列  \n",
    "dq = deque()\n",
    "\n",
    "print(\"在队列的右侧添加元素\")  \n",
    "dq.append(5)  \n",
    "dq.append(2)\n",
    "print(dq)\n",
    "  \n",
    "print(\"在队列的左侧添加元素\")  \n",
    "dq.appendleft(1)\n",
    "print(dq)\n",
    "  \n",
    "print(\"从队列的右侧删除元素并返回该元素\")  \n",
    "right_element = dq.pop()  \n",
    "print(right_element) \n",
    "  \n",
    "print(\"从队列的左侧删除元素并返回该元素\")  \n",
    "left_element = dq.popleft()  \n",
    "print(left_element)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小帅发送消息给小美：你好，小美！\n",
      "小美收到消息：你好，小美！\n",
      "小美的消息队列内容：['你好，小美！']\n",
      "小美收到消息：你好，小美！\n",
      "小美的最终消息队列内容：[]\n"
     ]
    }
   ],
   "source": [
    "from queue import Queue\n",
    "\n",
    "class WeChatUser:\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "        self.message_queue = Queue()\n",
    "\n",
    "    def send_message(self, message, recipient):\n",
    "        print(f\"{self.name}发送消息给{recipient.name}：{message}\")\n",
    "        recipient.receive_message(message)\n",
    "\n",
    "    def receive_message(self, message):\n",
    "        print(f\"{self.name}收到消息：{message}\")\n",
    "        self.message_queue.put(message)\n",
    "        print(f\"{self.name}的消息队列内容：{list(self.message_queue.queue)}\")\n",
    "\n",
    "def main():\n",
    "    # 创建两个用户\n",
    "    xiaomei = WeChatUser(\"小美\")\n",
    "    xiaoshuai = WeChatUser(\"小帅\")\n",
    "\n",
    "    # 小帅发送消息给小美\n",
    "    xiaoshuai.send_message(\"你好，小美！\", xiaomei)\n",
    "\n",
    "    # 模拟消息队列\n",
    "    while not xiaomei.message_queue.empty():\n",
    "        message = xiaomei.message_queue.get()\n",
    "        print(f\"{xiaomei.name}收到消息：{message}\")\n",
    "\n",
    "    # 显示最终消息队列状态\n",
    "    print(f\"{xiaomei.name}的最终消息队列内容：{list(xiaomei.message_queue.queue)}\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "True\n",
      "False\n",
      "True\n",
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "class CircularQueue:  \n",
    "    def __init__(self, k: int):  \n",
    "        self.k = k  # 循环队列的容量  \n",
    "        self.queue = [None] * k  # 初始化循环队列  \n",
    "        self.head = -1  # 队头指针，初始值为-1，表示队列为空  \n",
    "        self.tail = -1  # 队尾指针，初始值为-1，表示队列为空  \n",
    "  \n",
    "    def enqueue(self, value: int) -> bool:  \n",
    "        if self.is_full():  \n",
    "            return False  # 队列已满，无法进队  \n",
    "        if self.is_empty():  \n",
    "            self.head = 0  # 如果队列为空，将队头指针指向第一个元素的位置  \n",
    "        self.tail = (self.tail + 1) % self.k  # 计算新的队尾指针位置  \n",
    "        self.queue[self.tail] = value  # 在队尾插入元素  \n",
    "        return True  \n",
    "  \n",
    "    def dequeue(self) -> bool:  \n",
    "        if self.is_empty():  \n",
    "            return False  # 队列为空，无法出队  \n",
    "        if self.head == self.tail:  # 如果队列中只有一个元素，出队后将队头和队尾指针都指向-1，表示队列为空  \n",
    "            self.head = -1  \n",
    "            self.tail = -1  \n",
    "            return True  \n",
    "        self.head = (self.head + 1) % self.k  # 计算新的队头指针位置  \n",
    "        return True  \n",
    "  \n",
    "    def is_empty(self) -> bool:  \n",
    "        return self.head == -1 and self.tail == -1  # 如果队头和队尾指针都指向-1，表示队列为空  \n",
    "  \n",
    "    def is_full(self) -> bool:  \n",
    "        return (self.tail + 1) % self.k == self.head  # 如果队列中下一个元素的位置是队头指针的位置，表示队列已满\n",
    "\n",
    "cq = CircularQueue(3)  # 创建一个容量为3的循环队列  \n",
    "print(cq.enqueue(\"张三\"))  # True  \n",
    "print(cq.enqueue(\"李四\"))  # True  \n",
    "print(cq.enqueue(\"王五\"))  # True\n",
    "print(cq.enqueue(\"赵六\"))  # False，队列已满，无法进队     \n",
    "print(cq.dequeue())  # True，出队1  \n",
    "print(cq.dequeue())  # True，出队2  \n",
    "print(cq.dequeue())  # True，出队3\n",
    "print(cq.dequeue())  # False，队列为空，无法出队"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Stack:  \n",
    "    def __init__(self):  \n",
    "        self.stack = []  \n",
    "    \n",
    "    #入栈操作\n",
    "    def push(self, item):  \n",
    "        self.stack.append(item)  \n",
    "    #出栈操作\n",
    "    def pop(self):  \n",
    "        if not self.is_empty():  \n",
    "            return self.stack.pop()  \n",
    "        else:  \n",
    "            return None  \n",
    "    \n",
    "    #查看栈顶元素\n",
    "    def peek(self):  \n",
    "        if not self.is_empty():  \n",
    "            return self.stack[-1]  \n",
    "        else:  \n",
    "            return None  \n",
    "  \n",
    "    #判断栈是否为空\n",
    "    def is_empty(self):  \n",
    "        return len(self.stack) == 0  \n",
    "\n",
    "    #查看栈的尺寸\n",
    "    def size(self):  \n",
    "        return len(self.stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Browser:  \n",
    "    def __init__(self):  \n",
    "        self.history = []  \n",
    "        self.current_page = None  \n",
    "      \n",
    "    def visit_page(self, page):  \n",
    "        self.history.append(self.current_page)  \n",
    "        self.current_page = page  \n",
    "      \n",
    "    def go_back(self):  \n",
    "        if len(self.history) > 0:  \n",
    "            self.current_page = self.history.pop()  \n",
    "        else:  \n",
    "            print(\"Can't go back anymore.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 Hello 3.14 [1, 2, 3] \n"
     ]
    }
   ],
   "source": [
    "class ListNode:  \n",
    "    \"\"\"链表节点类\"\"\"  \n",
    "    def __init__(self, value=None):  \n",
    "        self.value = value  # 节点存储的值，可以是任意类型  \n",
    "        self.next = None    # 指向下一个节点的指针  \n",
    "  \n",
    "class LinkedList:  \n",
    "    \"\"\"单向链表类\"\"\"  \n",
    "    def __init__(self):  \n",
    "        self.head = None  # 链表头节点  \n",
    "  \n",
    "    def append(self, value):  \n",
    "        \"\"\"在链表末尾添加一个新节点\"\"\"  \n",
    "        if not self.head:  \n",
    "            self.head = ListNode(value)  \n",
    "        else:  \n",
    "            current = self.head  \n",
    "            while current.next:  \n",
    "                current = current.next  \n",
    "            current.next = ListNode(value)  \n",
    "  \n",
    "    def display(self):  \n",
    "        \"\"\"打印链表中的所有元素\"\"\"  \n",
    "        current = self.head  \n",
    "        while current:  \n",
    "            print(current.value, end=' ')  \n",
    "            current = current.next  \n",
    "        print()  # 换行  \n",
    "  \n",
    "# 使用示例  \n",
    "if __name__ == \"__main__\":  \n",
    "    ll = LinkedList()  \n",
    "    ll.append(1)  \n",
    "    ll.append(\"Hello\")  \n",
    "    ll.append(3.14)  \n",
    "    ll.append([1, 2, 3])  \n",
    "    ll.display()  # 输出: 1 Hello 3.14 [1, 2, 3]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
